package com.csr.csrcheck.service.impl;

import com.csr.csrcheck.mapper.CompanyMapper;
import com.csr.csrcheck.pojo.Company;
import com.csr.csrcheck.pojo.Special_bulletin;
import com.csr.csrcheck.service.CompanyService;
import com.csr.csrcheck.service.ex.CompanyException;
import com.csr.csrcheck.util.PageResult;
import com.csr.csrcheck.util.PageUtils;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

@Service
public class CompanyServiceImpl implements CompanyService {
    @Resource
    private CompanyMapper companyMapper;

    public List<Company> getCommpanylist() {
        List<Company> list = this.companyMapper.getCommpanylist();
        if (list == null)
            throw new CompanyException("");
        return list;
    }

    public List<Company> getCommpanylistbyname(String company_name) {
        List<Company> list = this.companyMapper.getCommpanylistbyname(company_name);
        Company company = new Company();
        if (list == null)
            throw new CompanyException("");
        if (company_name.equals("") || company_name == null)
            throw new CompanyException("");
        return list;
    }

    public PageResult findPage(int pageNum, int pageSize, String company_name) {
        return PageUtils.getPageResult(pageNum, pageSize, getPageinfo(pageNum, pageSize, company_name));
    }

    public List<Company> getCommpanylistpage(String company_name, Integer currentPageNo, Integer pageSize) {
        List<Company> list = this.companyMapper.getCommpanylistpage(company_name, Integer.valueOf((currentPageNo.intValue() - 1) * pageSize.intValue()), pageSize);
        if (list == null)
            throw new CompanyException("");
        return list;
    }

    public int getCompanyCount(String company_name) {
        int count = this.companyMapper.getCompanyCount(company_name);
        return count;
    }

    public List<Company> getCommpanyByid() {
        return this.companyMapper.getCommpanyByid();
    }

    public List<Company> list() {
        List<Company> list = this.companyMapper.list();
        if (list == null)
            throw new CompanyException("");
        return list;
    }

    public void addCompany(Company company) {
        int row = this.companyMapper.addCommpany(company);
        if (row != 1)
            throw new CompanyException("");
    }

    public int updateCompany(Company company) {
        int row = this.companyMapper.updateCompany(company);
        if (row != 1)
            throw new CompanyException("");
        return row;
    }

    public Company findcompanyByid(int id) {
        Company list = this.companyMapper.findcompanyByid(id);
        if (list == null)
            throw new CompanyException("");
        return list;
    }

    public int deletecompanyByid(int id) {
        int row = this.companyMapper.deletecompanyByid(id);
        if (row != 1)
            throw new CompanyException("");
        return row;
    }

    private PageInfo<Company> getPageinfo(int pageNum, int pageSize, String company_name) {
        PageHelper.startPage(pageNum, pageSize);
        List<Company> companyList = this.companyMapper.getCompanyPage(company_name);
        return new PageInfo(companyList);
    }

    public List<Special_bulletin> getAll() {
        return this.companyMapper.getAll();
    }
}